home *** CD-ROM | disk | FTP | other *** search
/ The Best of MacTutor - S…e Code for Volumes 1 to 5 / The Best of MacTutor - Source Code for Volume 1-5 (Wayzata Technology)(6031)(1990).bin / Source Code / #43 (Apr 89) / Designer CDEF Code / ExampleWindow.Pas < prev    next >
Pascal/Delphi Source File  |  1989-01-09  |  11KB  |  294 lines

  1. UNIT ExampleWindow;
  2.  
  3. {File name: ExampleWindow.Pas}
  4. {Function: Handle a Window}
  5. {History: 12/19/88 Original by Prototyper.   }
  6. {Modified 1/6/89 optimized a little}
  7.  
  8. INTERFACE
  9.     USES
  10.         myCDEF;
  11.  
  12.   {Initialize us so all our routines can be activated}
  13.     PROCEDURE Init_ExampleWindow;
  14.  
  15.   {Open our window and draw everything}
  16.     PROCEDURE Open_ExampleWindow (VAR theWindow : WindowPtr);
  17.  
  18.   {Update our window, someone uncovered a part of us}
  19.     PROCEDURE Update_ExampleWindow (whichWindow : WindowPtr);
  20.  
  21.   {Handle action to our window, like controls}
  22.     PROCEDURE Do_ExampleWindow (myEvent : EventRecord);
  23.  
  24. IMPLEMENTATION
  25.  
  26.     CONST
  27.         I_Buttonx1 = 3;                     {Button ID}
  28.         I_Checkboxx2 = 4;                   {Checkbox ID}
  29.         I_Radio2 = 6;                      {Radio ID}
  30.         I_Radio3 = 7;                      {Radio ID}
  31.         I_Radio1 = 5;                       {Radio ID}
  32.     VAR
  33.         MyWindow : WindowPtr;                 {Window pointer}
  34.         tempRect : Rect;                    {Temporary rectangle}
  35.         CtrlHandle, buttonHdl : controlhandle;         {Control handle}
  36.         sTemp : Str255;                     {Get text entered, temp holding}
  37.         R1Control : ARRAY[1..3] OF ControlHandle; { Radio button handles for group 1}
  38.  
  39. {=================================}
  40.  
  41.   {Initialize us so all our routines can be activated}
  42.     PROCEDURE Init_ExampleWindow;
  43.  
  44.     BEGIN                                 {Start of Window initialize routine}
  45.         MyWindow := NIL;                      {Make sure other routines know we are not valid yet}
  46.     END;                                  {End of procedure}
  47.  
  48. {=================================}
  49.  
  50.   {Update our window, someone uncovered a part of us}
  51.     PROCEDURE UpDate_ExampleWindow;
  52.         CONST
  53.             picBaseID = 144;
  54.  
  55.         VAR
  56.             SavePort : WindowPtr;               {Place to save the last port}
  57.             pictRect : rect;
  58.             Pic_Handle : PicHandle;
  59.             pictindex : integer;
  60.  
  61.     BEGIN                                 {Start of Window update routine}
  62.         IF (MyWindow <> NIL) AND (MyWindow = whichWindow) THEN {Handle an open when already opened}
  63.             BEGIN
  64.                 GetPort(SavePort);                {Save the current port}
  65.                 SetPort(MyWindow);                {Set the port to my window}
  66.                 TextFont(systemFont);             {Set the font to draw in}
  67.       {Draw a string of text, Static Text }
  68.                 SetRect(tempRect, 266, 100, 312, 116);
  69.                 sTemp := 'Button';
  70.                 TextBox(Pointer(ord(@sTemp) + 1), length(sTemp), tempRect, teJustLeft);
  71.  
  72.       {Draw a string of text, Static Text }
  73.                 SetRect(tempRect, 151, 100, 217, 116);
  74.                 sTemp := 'CheckBox';
  75.                 TextBox(Pointer(ord(@sTemp) + 1), length(sTemp), tempRect, teJustLeft);
  76.  
  77.       {Draw a string of text, Static Text }
  78.                 SetRect(tempRect, 36, 100, 77, 116);
  79.                 sTemp := 'Radio';
  80.                 TextBox(Pointer(ord(@sTemp) + 1), length(sTemp), tempRect, teJustLeft);
  81.  
  82.       {Draw a string of text, Static Text }
  83.                 SetRect(tempRect, 76, 125, 287, 141);
  84.                 sTemp := 'Designer CDEF by Kirk Chase';
  85.                 TextBox(Pointer(ord(@sTemp) + 1), length(sTemp), tempRect, teJustLeft);
  86.                 TextFont(applFont);               {Set the default application font}
  87.  
  88.                 pictRect := buttonHdl^^.contrlRect;
  89.                 InsetRect(pictRect, 2, 2);
  90.                 pictindex := GetCtlValue(buttonHdl);
  91.                 Pic_Handle := GetPicture(picBaseID + pictindex);    {Get Picture into memory}
  92.                 IF (Pic_Handle <> NIL) THEN       {Only use handle if it is valid}
  93.                     DrawPicture(Pic_Handle, pictRect);
  94.  
  95.                 DrawControls(MyWindow);           {Draw all the controls}
  96.                 SetPort(SavePort);                {Restore the old port}
  97.             END;                                {End for if (MyWindow<>nil)}
  98.     END;                                  {End of procedure}
  99.  
  100. {=================================}
  101.  
  102.   {Open our window and draw everything}
  103.     PROCEDURE Open_ExampleWindow;
  104.         VAR
  105.             CDEFHandle : handle;
  106.             chkhdl : controlhandle;
  107.  
  108.     BEGIN                                 {Start of Window open routine}
  109.  
  110.         IF (MyWindow = NIL) THEN            {Handle an open when already opened}
  111.             BEGIN
  112.                 MyWindow := GetNewWindow(1, NIL, Pointer(-1)); {Get the window from the resource file}
  113.                 theWindow := MyWindow;
  114.  
  115.         {get a handle to our CDEF Proc}
  116.                 CDEFHandle := NewHandle(0);
  117.                 CDEFHandle^ := Ptr(@DesignerButtonProc);
  118.  
  119.       { Make a button, Button }
  120.                 CtrlHandle := GetNewControl(I_Buttonx1, MyWindow);
  121.                 HLock(Handle(CtrlHandle));
  122.                 CtrlHandle^^.contrlDefProc := CDEFHandle; {give in our handle}
  123.                 HUnLock(Handle(CtrlHandle));
  124.                 CtrlHandle^^.contrlHilite := 255;
  125.                 buttonHdl := CtrlHandle;
  126.  
  127.       { Make a checkbox, Checkbox }
  128.                 CtrlHandle := GetNewControl(I_Checkboxx2, MyWindow); {Make a new checkbox}
  129.                 HLock(Handle(CtrlHandle));
  130.                 CtrlHandle^^.contrlDefProc := CDEFHandle; {give in our handle}
  131.                 HUnLock(Handle(CtrlHandle));
  132.                 chkhdl := CtrlHandle;
  133.  
  134.  
  135.       { Make a radio button, Radio1 }
  136.                 R1Control[1] := GetNewControl(I_Radio1, MyWindow);
  137.                 HLock(Handle(CtrlHandle));
  138.                 R1Control[1]^^.contrlDefProc := CDEFHandle; {give in our handle}
  139.                 HUnLock(Handle(CtrlHandle));
  140.  
  141.       { Make a radio button, Radio2 }
  142.                 R1Control[2] := GetNewControl(I_Radio2, MyWindow);
  143.                 HLock(Handle(CtrlHandle));
  144.                 R1Control[2]^^.contrlDefProc := CDEFHandle; {give in our handle}
  145.                 HUnLock(Handle(CtrlHandle));
  146.  
  147.       { Make a radio button, Radio3 }
  148.                 R1Control[3] := GetNewControl(I_Radio3, MyWindow);
  149.                 HLock(Handle(CtrlHandle));
  150.                 R1Control[3]^^.contrlDefProc := CDEFHandle; {give in our handle}
  151.                 HUnLock(Handle(CtrlHandle));
  152.  
  153.                 ShowWindow(MyWindow);
  154.                 SelectWindow(MyWindow);           {Bring our window to the front}
  155.                 SetPort(MyWindow);                {Prepare to write into our window}
  156.  
  157.                 UpDate_ExampleWindow(MyWindow);   {Do an update to draw rest of items}
  158.  
  159.             END                               {End for if (MyWindow<>nil)}
  160.         ELSE
  161.             SelectWindow(MyWindow);           {Already open, so show it}
  162.  
  163.     END;                                  {End of procedure}
  164.  
  165. {=================================}
  166.  
  167.   {Handle action to our window, like controls}
  168.     PROCEDURE Do_ExampleWindow;
  169.         CONST
  170.             HiliteValue = 10;
  171.             inactive = 255;
  172.             active = 0;
  173.             left = 0;
  174.             right = 1;
  175.  
  176.         VAR
  177.             RefCon : integer;                     {RefCon for controls}
  178.             code : integer;                       {Location of event in window or controls}
  179.             theValue : integer;                   {Current value of a control}
  180.             whichWindow : WindowPtr;              {Window pointer where event happened}
  181.             myPt, myPtGlobal : Point;                         {Point where event happened}
  182.             theControl : ControlHandle;           {Handle for a control}
  183.             badRect : rect;
  184.  
  185.         PROCEDURE Do_A_Button;                {Handle a button being pressed}
  186.  
  187.         BEGIN
  188.             HiliteControl(theControl, HiliteValue);      {Darken the button}
  189.             RefCon := GetCRefCon(theControl);   {get control refcon}
  190.  
  191.             CASE RefCon OF                      {Select correct button}
  192.                 I_Buttonx1 :                 {Button, button}
  193.                     BEGIN                     {start for this button}
  194.                         theValue := GetCtlValue(theControl); {Get current value}
  195.                         theValue := (theValue + 1) MOD 6;   {Change value}
  196.                         SetCtlValue(theControl, theValue); {Set button to new value}
  197.                         badRect := buttonhdl^^.contrlRect;
  198.                         InvalRect(badRect); {invalidate for new die picture}
  199.                     END;                      {end for this button}
  200.  
  201.                 OTHERWISE                     {allow other buttons, trap for debug}
  202.                     ;
  203.             END;                                {end of case}
  204.  
  205.             HiliteControl(theControl, active);       {Lighten the button}
  206.         END;                                  {Handle a button being pressed}
  207.  
  208.  
  209.         PROCEDURE Do_A_Checkbox;              {Handle a checkbox being pressed}
  210.             VAR
  211.                 Index : integer;                      {Index used for radios}
  212.  
  213.             PROCEDURE Clear1RadioGroup;           {Routine to clear radios in group 1}
  214.                 VAR
  215.                     Index : integer;                      {Index used for radios}
  216.             BEGIN                                 {Start of the clear routine}
  217.                 FOR Index := 1 TO 3 DO              {Step thru all radios}
  218.                     SetCtlValue(R1Control[Index], 0);  {Set this radio to zero}
  219.             END;                                  {End of the clear routine}
  220.  
  221.         BEGIN                                 {Handle a checkbox being pressed}
  222.             RefCon := GetCRefCon(theControl);   {get control refcon}
  223.             theValue := GetCtlValue(theControl); {Get current value}
  224.             theValue := (theValue + 1) MOD 2;   {Change value}
  225.  
  226.             CASE RefCon OF                      {Select correct button}
  227.                 I_Checkboxx2 :               {Checkbox, checkbox}
  228.                     BEGIN                     {start for this button}
  229.                         SetCtlValue(theControl, theValue); {Set checkbox to new value}
  230.                         badRect := buttonhdl^^.contrlRect;
  231.                         InvalRect(badRect);
  232.                         CASE theValue OF {activate left or right controls}
  233.                             left : 
  234.                                 BEGIN
  235.                                     HiliteControl(buttonHdl, inactive);
  236.                                     HiliteControl(R1Control[1], active);
  237.                                     HiliteControl(R1Control[2], active);
  238.                                     HiliteControl(R1Control[3], active);
  239.                                 END;
  240.                             right : 
  241.                                 BEGIN
  242.                                     HiliteControl(buttonHdl, active);
  243.                                     HiliteControl(R1Control[1], inactive);
  244.                                     HiliteControl(R1Control[2], inactive);
  245.                                     HiliteControl(R1Control[3], inactive);
  246.                                 END;
  247.                             OTHERWISE
  248.                                 ;
  249.                         END;
  250.                     END;                      {end for this checkbox}
  251.  
  252.                 I_Radio1, I_Radio2, I_Radio3 :                   {radio buttons}
  253.                     BEGIN                    {start for this radio button}
  254.                         Clear1RadioGroup;       {Clear all Radio values in this group}
  255.                         SetCtlValue(theControl, 1); {Select this Radio}
  256.                     END;                      {end for this radio button}
  257.  
  258.                 OTHERWISE
  259.                     ;
  260.             END;                                {end of case}
  261.  
  262.         END;                                  {Handle a checkbox being pressed}
  263.  
  264.  
  265.     BEGIN                                 {Start of Window handler}
  266.         IF (MyWindow <> NIL) THEN           {Handle only when the window is valid}
  267.             BEGIN
  268.                 code := FindWindow(myEvent.where, whichWindow); {Get where in window and which window}
  269.  
  270.                 IF (myEvent.what = MouseDown) AND (MyWindow = whichWindow) THEN {}
  271.                     BEGIN                           {}
  272.                         myPt := myEvent.where;          {Get mouse position}
  273.                         myPtGlobal := myPt;
  274.                         GlobalToLocal(myPt);
  275.                     END;
  276.  
  277.                 IF (MyWindow = whichWindow) AND (code = inContent) THEN {for our window}
  278.                     BEGIN
  279.  
  280.                         code := FindControl(myPt, whichWindow, theControl); {Get type of control}
  281.                         IF (code <> 0) THEN             {Check type of control}
  282.                             code := TrackControl(theControl, myPt, NIL); {Track the control}
  283.                         IF code = inButton THEN
  284.                             Do_A_Button;                  {Do buttons}
  285.                         IF code = inCheckBox THEN
  286.                             Do_A_Checkbox;                {Do checkboxes}
  287.  
  288.                     END;                            {End for if (MyWindow=whichWindow)}
  289.             END;                              {End for if (MyWindow<>nil)}
  290.     END;                                  {End of procedure}
  291.  
  292. {=================================}
  293.  
  294. END.                                    {End of unit}